package com.weilele.leisure.time.ui.holder


import android.view.View
import com.google.android.exoplayer2.offline.Download
import com.weilele.leisure.time.R
import com.weilele.leisure.time.VIDEO_ID_OTHER_DOWNLOAD
import com.weilele.leisure.time.VIDEO_ID_SD_CARD
import com.weilele.leisure.time.bean.db.HistoryVideoBean
import com.weilele.leisure.time.databinding.HolderVideoCacheBinding
import com.weilele.leisure.time.ui.module.video_cache.VideoCacheDetailFragment
import com.weilele.leisure.time.ui.module.video_cache.VideoOtherCacheActivity
import com.weilele.leisure.video_factory.ui.module.local_video.LocalVideoFragment
import com.weilele.mvvm.adapter.MvvmHolder
import com.weilele.mvvm.adapter.mvvmRcvAdapter
import com.weilele.mvvm.utils.activity.getActivity
import com.weilele.mvvm.utils.activity.onClick
import com.weilele.mvvm.utils.activity.textFrom
import com.weilele.mvvm.utils.glide.setImageAny
import com.weilele.mvvm.utils.keepTwoPoint


class VideoCacheHolder :
    MvvmHolder<Pair<HistoryVideoBean, List<Download>>, HolderVideoCacheBinding> {
    companion object {
        fun toAdapter() = mvvmRcvAdapter(
            VideoCacheHolder::class.java,
            { old, new -> old?.first?.videoId == new?.first?.videoId },
            ::onContentSame
        )

        private fun onContentSame(
            old: Pair<HistoryVideoBean, List<Download>>?,
            new: Pair<HistoryVideoBean, List<Download>>?
        ): Boolean {
            val oldData = old?.second
            val newData = new?.second
            val oldCount = oldData?.count()
            val newCount = oldData?.count()
            if (oldCount != newCount) {
                return false
            }
            var oldProgress = 0f
            oldData?.forEach {
                oldProgress += it.percentDownloaded
            }
            var newProgress = 0f
            newData?.forEach {
                newProgress += it.percentDownloaded
            }
            return oldProgress == newProgress
        }
    }

    init {
        itemView.onClick { view ->
            dataWhenBind?.first?.let {
                when (it.videoId) {
                    VIDEO_ID_OTHER_DOWNLOAD -> {
                        val ids = dataWhenBind?.second?.map { it.request.id }?.toTypedArray()
                        VideoOtherCacheActivity.start(view, ids)
                    }
                    VIDEO_ID_SD_CARD -> {
                        LocalVideoFragment.start(view.getActivity())
                    }
                    else -> {
                        VideoCacheDetailFragment.start(view.getActivity(), it)
                    }
                }
            }
        }
    }

    constructor(binding: HolderVideoCacheBinding) : super(binding)
    constructor(itemView: View) : super(itemView)

    override fun bindData(data: Pair<HistoryVideoBean, List<Download>>) {
        val cacheBean = data.first
        mBinding.ivCover.setImageAny(cacheBean.picUrl)
        mBinding.name.text = cacheBean.title
        when (cacheBean.videoId) {
            VIDEO_ID_SD_CARD -> {
                mBinding.tvProgress.textFrom(R.string.had_downloaded)
            }
            else -> {
                updateProgress(data.second)
            }
        }
    }

    private fun updateProgress(data: List<Download>) {
        var progress = 0f
        val count = data.count()
        data.forEach {
            progress += it.percentDownloaded
        }
        mBinding.tvProgress.text = "共${count}视频\n总进度\n${(progress / count).keepTwoPoint()}%"
    }
}